package com.dq.controller.user;

import com.dq.context.Constant;
import com.dq.controller.in.request.UserSchedulingRequest;
import com.dq.controller.in.user.BossUserQuery;
import com.dq.controller.in.user.SysUserQuery;
import com.dq.controller.in.user.WxUserQuery;
import com.dq.controller.sys.SysBackUserSectionController;
import com.dq.domain.org.Org;
import com.dq.domain.sys.role.SysRole;
import com.dq.domain.sys.role.SysUserRole;
import com.dq.domain.sys.user.UserAccount;
import com.dq.domain.sys.user.center.UserAccountStaffUser;
import com.dq.domain.sys.user.user.BossUser;
import com.dq.domain.sys.user.user.OrgUser;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.centre.UserLableCentre;
import com.dq.exception.customException.AdminException;
import com.dq.service.AllService;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author cf
 */

@RestController
@RequestMapping("/sys/orguser/")
@Api(description = "门店用户管理")
public class SysOrgUserController extends AllService {
    @Autowired
    private SysBackUserSectionController sysBackUserSectionController;

    //    单个门店
    @PostMapping("userListAll")
    @ApiOperation("门店所有用户列表-店长/员工")
    public Vo userListAll(@RequestBody SysUserQuery sysUserQuery) throws AdminException {
        sysUserQuery.format();
        sysUserQuery.setPage(null);
        sysUserQuery.setSize(null);
        List<BossUser> list = orgUserService.getOrgUserListByRoles(sysUserQuery, 3, 4);
        return new ListDataVo((List<BossUser.Vo>) VO.toVOCollection(list));
    }

    @PostMapping("userList")
    @ApiOperation("门店所有用户列表-分页")
    public Vo userList(@RequestBody SysUserQuery sysUserQuery) throws AdminException {
        sysUserQuery.format();

        int[] ints = {3, 4};
        List<BossUser> list = orgUserService.getOrgUserListByRoles(sysUserQuery, ints);
        return new ListDataVo((List<BossUser.Vo>) VO.toVOCollection(list), orgUserService.getOrgUserCountByRoles(sysUserQuery, ints));
    }

    @PostMapping("userRoleList")
    @ApiOperation("门店所有用户列表-分页")
    public Vo userRoleList(@RequestBody SysUserQuery sysUserQuery) throws AdminException {
        sysUserQuery.format();


        int[] ints = {3, 4};
        List<BossUser> list = orgUserService.getOrgUserListByRoles(sysUserQuery, ints);
        List<Long> userIds = new ArrayList<>();
        for (BossUser bossUser : list) {
            userIds.add(bossUser.getId());
        }
        List<SysUserRole> roles = sysUserRoleRepository.findByDeletedIsFalseAndSysUserIdIn(userIds);
        List<BossUser.Vo> returnList = new ArrayList<>();

        for (BossUser bossUser : list) {
            BigDecimal sumCommission = BigDecimal.ZERO;
            List<UserAccountStaffUser> userAccountStaffUsers = userAccountStaffUserRepository.findByDeletedFalseAndStaffUser(bossUser);
            for(UserAccountStaffUser userAccountStaffUser : userAccountStaffUsers){
                sumCommission = sumCommission.add(userAccountStaffUser.getAchievements());
            }
            BossUser.Vo vo = (BossUser.Vo) bossUser.toVO();
            vo.setSumCommission(sumCommission);
            Set<SysRole.Vo> roleList = vo.getRoleList();
            for (SysUserRole role : roles) {
                if (role.getSysUser().getId().toString().equals(bossUser.getId().toString())) {
                    roleList.add((SysRole.Vo) role.getSysRole().toVO());
                }
            }
            returnList.add(vo);
        }
        return new ListDataVo(returnList, orgUserService.getOrgUserCountByRoles(sysUserQuery, ints));
    }

    // 所有门店
    @PostMapping("userShopListAll")
    @ApiOperation("所有门店的店长列表")
    public Vo userShopListAll(@RequestBody SysUserQuery sysUserQuery) throws AdminException {
        sysUserQuery.format();
        sysUserQuery.setPage(null);
        sysUserQuery.setSize(null);

        List<BossUser> list = bossUserService.getBossUserListByRoles(sysUserQuery, 3);
        return new ListDataVo((List<BossUser.Vo>) VO.toVOCollection(list));
    }

    @PostMapping("add")
    @ApiOperation("门店用户添加")
    public Vo orgUserAdd(@RequestBody BossUser bossUser) throws AdminException {
        if (StringUtils.isBlank(bossUser.getRoleIds())) {
            List<String> roleCodes = new ArrayList<>();
            roleCodes.add(Constant.ROLE_CODE_LIST.get(4));
            List<SysRole> roleList = sysRoleRepository.findByCodeIn(roleCodes);

            String roleIds = "";
            for (SysRole sysRole : roleList) {
                roleIds = roleIds + sysRole.getId() + ",";
            }
            if (StringUtils.isNotBlank(roleIds)) {
                roleIds = roleIds.substring(0, roleIds.length() - 1);
            }
            bossUser.setRoleIds(roleIds);
        }
        sysBackUserSectionController.save(bossUser);

        return new Vo(HttpStatus.OK);
    }

    @PostMapping("edit")
    @ApiOperation("门店用户修改")
    public Vo orgUserEdit(@RequestBody BossUser bossUser) throws AdminException {
        if (bossUser.getId() == null) {
            throw new AdminException("ID为空");
        }
        sysBackUserSectionController.update(bossUser, bossUser.getId());

        return new Vo(HttpStatus.OK);
    }

    @DeleteMapping("delete/{ids}")
    @ApiOperation("门店用户删除")
    public Vo orgUserDelete(@PathVariable @ApiParam(value = "用户id") Long[] ids) throws AdminException {
        sysBackUserSectionController.deletes(ids);

        return new Vo(HttpStatus.OK);
    }

    @PostMapping("userScheduling")
    @ApiOperation("用户排班")
    public Vo userScheduling(@RequestBody UserSchedulingRequest userSchedulingRequest) throws AdminException {

        return bossUserService.userScheduling(userSchedulingRequest);
    }


    @ApiOperation("辞退")
    @PostMapping("dismiss/{id}")
    public Vo dismiss(@PathVariable Long id) throws AdminException {
        return bossUserService.dismiss(id);
    }

    @ApiOperation("转移")
    @PostMapping("transfer/{id}/{orgId}")
    public Vo transfer(@PathVariable Long id ,@PathVariable Long orgId) throws AdminException{
        return bossUserService.transfer(id, orgId);
    }

    // 所有门店
    @PostMapping("teacherShopListAll")
    @ApiOperation("所有门店的带单老师列表")
    public Vo teacherShopListAll(@RequestBody SysUserQuery sysUserQuery) throws AdminException {
        sysUserQuery.format();
        sysUserQuery.setPage(null);
        sysUserQuery.setSize(null);

        List<BossUser> list = bossUserService.getBossUserListByRoles(sysUserQuery, 5);
        return new ListDataVo((List<BossUser.Vo>) VO.toVOCollection(list));
    }
}

